#include "rt_AcqTrans_proc.h"

void rt_AcqTrans_proc(XRt1553B *InstancePtr, XRt1553B_MsgBlkDesc *CMDInfo, B1553_BusService *pBusServ)
{
    u16 foo1 = 0, foo2 = 0;

    if (CMDInfo->T_or_R == 1) // BC每帧取ATR 作为超时计数 并判断
    {
        pBusServ->AcqTransTimeOut++;
        // 非复位状态 出现超时 进行协议复位
        if ((pBusServ->AcqTransTimeOut > CCSDS_ATC_TIMEOUT) && (1 == pBusServ->AcqTransReady))
        {
            pBusServ->AcqTransReq_AcqBlkCnt = 0; // ATR中编号置0
            pBusServ->AcqTransReq_ctrl = CCSDS_ATR_RESET | (pBusServ->AcqTransReq_AcqBlkCnt);
            pBusServ->AcqTransReq_bytes = 0;
            rt_ATRWrite(InstancePtr, pBusServ);
            pBusServ->AcqTransReady = 0;
            pBusServ->AcqTransTimeOut = 0; // 重置超时计数
        }
        return;
    }

    // 接收到数据了
    rt_ATCRead(InstancePtr, pBusServ);
    if (0 == pBusServ->AcqTransReady) // 当前为复位状态
    {
        // 收到复位确认 退出复位状态
        if ((0 == pBusServ->AcqTransCfm_bytes) && (CCSDS_ATC_RESET == pBusServ->AcqTransCfm_ctrl))
        {
            pBusServ->AcqTransReq_AcqBlkCnt = 0; // ATR中编号置0
            pBusServ->AcqTransReq_ctrl = CCSDS_TRCTRL_RUNNING | (pBusServ->AcqTransReq_AcqBlkCnt);
            pBusServ->AcqTransReq_bytes = 0;
            rt_ATRWrite(InstancePtr, pBusServ);
            pBusServ->AcqTransReady = 1; // 握手成功 设置标志
        }
        else // 没有收到复位允许 保持复位状态
        {
            pBusServ->AcqTransReq_AcqBlkCnt = 0; // ATR中编号置0
            pBusServ->AcqTransReq_ctrl = CCSDS_ATR_RESET | (pBusServ->AcqTransReq_AcqBlkCnt);
            pBusServ->AcqTransReq_bytes = 0;
            rt_ATRWrite(InstancePtr, pBusServ);
            pBusServ->AcqTransReady = 0; // 保持复位状态
        }
        pBusServ->AcqTransTimeOut = 0; // 复位状态下初始化超时标志
        return;                        // 复位情况处理完成 函数退出
    }

    // 非复位状态 没有超时 收到ATC
    foo1 = (pBusServ->AcqTransCfm_ctrl) ^ (pBusServ->AcqTransReq_ctrl);
    foo2 = (pBusServ->AcqTransCfm_bytes) ^ (pBusServ->AcqTransReq_bytes);
    // 数据相等 ATC正确 遥测传输结束
    if ((foo1 + foo2) == 0) // 异或结果之和为0 表示数据均相等 恢复请求初始状态
    {
        pBusServ->AcqTransTimeOut = 0; // 重置超时标志
        pBusServ->AcqTransReq_bytes = 0;
        pBusServ->AcqTransReq_ctrl = CCSDS_TRCTRL_RUNNING;
        rt_ATRWrite(InstancePtr, pBusServ);
        pBusServ->AcqTransReady = 1;
    }

    return;
}

void rt_DistrbTrans_proc(XRt1553B *InstancePtr, XRt1553B_MsgBlkDesc *CMDInfo, B1553_BusService *pBusServ)
{
    u16 foo1 = 0;
    u16 foo2 = 0;

    if (CMDInfo->T_or_R == 1) // 不处理发送
    {
        return;
    }

    rt_DTDRead(InstancePtr, pBusServ);
    // DTD复位标记为1 DTC设置复位
    if ((CCSDS_DTD_RESET_MASK & pBusServ->DistrbTransDesc_ctrl) > 0)
    {
        pBusServ->DistrbTransCfm_bytes = 0;
        pBusServ->DistrbTransCfm_ctrl = CCSDS_DTC_RESET;
        rt_DTCWrite(InstancePtr, pBusServ);
        return; // 复位情况处理完成 函数退出
    }

    // 非复位 比较DTD中的分发块计数和当前DTC中的分发块计数 不同为新消息
    foo1 = pBusServ->DistrbTransCfm_ctrl & CCSDS_DTD_BLOCK_CNT_MASK;
    foo2 = pBusServ->DistrbTransDesc_ctrl & CCSDS_DTD_BLOCK_CNT_MASK;
    if (foo1 != foo2) // 不相等 有新数据
    {
        pBusServ->Msg_Rx_cnt++; // 接收计数+1
        pBusServ->DistrbTransCfm_bytes = pBusServ->DistrbTransDesc_bytes;
        pBusServ->DistrbTransCfm_ctrl = pBusServ->DistrbTransDesc_ctrl;
        rt_DTCWrite(InstancePtr, pBusServ);
    }
    // 相等则无新数据 不处理

    return;
}